var app = getApp()
var db = wx.cloud.database()
Page({
    data: {

    },
    // 上传图片
    addimg: function() {
        let that = this;
        // 选择图片
        wx.chooseImage({
            count: 3,
            sizeType: ['compressed'],
            sourceType: ['album', 'camera'],
            success: function(res) {
                console.log(res)
                wx.showLoading({
                    title: '上传中',
                })
                const filePath = res.tempFilePaths[0]
                console.log(filePath)
                let date = new Date();
                let h = date.getHours();
                let m = date.getMinutes();
                let s = date.getSeconds();


                // 上传图片
                const cloudPath = h + m + s + 'my-image' + filePath.match(/\.[^.]+?$/)[0]

                console.log(cloudPath)
                wx.cloud.uploadFile({
                    cloudPath,
                    filePath,
                    success: res => {
                        console.log('[上传文件] 成功：', res)
                        app.globalData.fileID = res.fileID
                        app.globalData.cloudPath = cloudPath
                        app.globalData.imagePath = filePath
                        that.setData({
                            addimg: res.fileID
                        })
                        wx.showModal({
                            title: '上传成功',
                            content: '图片已上传',
                            showCancel: true,
                            success: function(res) {},
                        })
                    },
                    fail: e => {
                        console.error('[上传文件] 失败：', e)
                        wx.showToast({
                            icon: 'none',
                            title: '上传失败',
                        })
                    },
                    complete: () => {
                        wx.hideLoading()
                    }
                })

            },
            fail: e => {
                console.error(e)
            }
        })
    },
    // 提交
    addBanner: function(e) {
        console.log(e)
        let that = this
        let name = e.detail.value.name
        let nav = e.detail.value.nav
        let img = that.data.addimg        
        if (img == "" || name==""){
            wx.showModal({
                title: '填写有误',
                content: '请上传图片并填写标题',
                showCancel: true,
            })
            return 
        }
        let key = that.data.key
        db.collection(key).add({
                data: {
                    name: name,
                    nav: nav,
                    img: img
                }
            })
            .then(res => {
                console.log(res)
                if (res.errMsg == "collection.add:ok") {
                    wx.showModal({
                        title: '保存成功',
                        content: '提交成功',
                        showCancel: true,
                        success: function(res) {
                            console.log(res.confirm)
                            if (res.confirm == true) {
                                wx.redirectTo({
                                    url: '/crm/pages/banner/banner',
                                })
                            }
                        },
                    })

                }
            })
    },

    onLoad: function(options) {
        let that = this
        console.log(options)
        let key = options.key
        that.setData({
            key:key
        })
    },

    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        let that = this;
        let winh = app.globalData.winH;
        that.setData({
            winh: winh
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})